<?php

function get_account_detail($id){
include(dirname(__FILE__) . '/../config.php');

$con=mysqli_connect($host,$user,$pass,$db);
mysqli_set_charset($con,"utf8");
// Check connection
if (mysqli_connect_errno()) {
  die ("Failed to connect to MySQL: " . mysqli_connect_error());
}
$id = mysql_real_escape_string($id);
$sql="SELECT * FROM accounts where id=".$id;

mysqli_query($con,"SET NAMES 'utf8'");	
$result = mysqli_query($con,$sql);
if(!$result){
    die(mysqli_error($con).':'.$sql);
}
$is_login=0;
$contact=array();
while($row = mysqli_fetch_array($result)) {
$contact=$row;    
}
return $contact;
mysqli_close($con);


}
function get_all_accounts($ids=null){
require('../config.php');
$con=mysqli_connect($host,$user,$pass,$db);
mysqli_set_charset($con,"utf8");
// Check connection
if (mysqli_connect_errno()) {
  die ("Failed to connect to MySQL: " . mysqli_connect_error());
}

$sql="SELECT * FROM accounts where 1";
$append_str='';
if($ids!=null&&is_array($ids)){
	
	$append_str.=implode(",", $ids);
$sql.=' AND id in ('.$append_str.')';
}

mysqli_query($con,"SET NAMES 'utf8'");	
$result = mysqli_query($con,$sql);
if(!$result){
    die(mysqli_error($con).':'.$sql);
}
$is_login=0;
$contacts=array();
while($row = mysqli_fetch_array($result)) {
$contacts[]=$row;    
}
mysqli_close($con);
return $contacts;

}

function add_account($post){
require('../config.php');
require('../util.php');
 $con=mysqli_connect($host,$user,$pass,$db);

// Check connection
if (mysqli_connect_errno()) {
  die ("Failed to connect to MySQL: " . mysqli_connect_error());
}
    $keys = array();
	$values = array();
	foreach ($post as $column_name=>$value) {
	if($column_name!='go'){
	$value = trim($value);
		$value = mysql_real_escape_string($value);
		$keys[] = "`{$column_name}`";
		$value = mysql_real_escape_string($value);
		$values[] = "'{$value}'";
	}
		
	}
	$query = "INSERT INTO accounts (" . implode(",", $keys) . ") VALUES (" . implode(",", $values). ");";
	//echo $query;
	mysqli_query($con,"SET NAMES 'utf8'");	
    mysqli_query($con, $query);
    mysqli_close($con);
}

function update_account($post){
require('../config.php');
require('../util.php');
 $con=mysqli_connect($host,$user,$pass,$db);

// Check connection
if (mysqli_connect_errno()) {
  die ("Failed to connect to MySQL: " . mysqli_connect_error());
}
    $keys = array();
	$values = array();
	$query ='Update accounts SET ';
	foreach ($post as $column_name=>$value) {
	if($column_name!='go'){
		$value = trim($value);
		$value = mysql_real_escape_string($value);
		$query.=" `{$column_name}` = "." '{$value}',";
	}
	
		
	}
	$query.="`id`=".$post['id'];	
	$query.=" Where id =".$post['id'];
	
	
	mysqli_query($con,"SET NAMES 'utf8'");	
    mysqli_query($con, $query);
	//echo 'query='.$query;
	//die();
    mysqli_close($con);
}

function delete_account($id){
	require('../config.php');
	require('../util.php');
	 $con=mysqli_connect($host,$user,$pass,$db);

	// Check connection
	if (mysqli_connect_errno()) {
	  die ("Failed to connect to MySQL: " . mysqli_connect_error());
	}

	$id = mysql_real_escape_string($id);    
	$query ='DELETE FROM accounts WHERE id = '.$id;
	mysqli_query($con,"SET NAMES 'utf8'");		
    mysqli_query($con, $query);
    mysqli_close($con);
}


?>